<?php

namespace Application\model;

use Cola\Model;

class ChannelModel extends Model
{

    public function init($platform,$config){
        $sql = "select * from ops_channels where cid = '{$platform}'";
        $row =  $this->db('_backdb')->row($sql);
        if($row){

        }


    }
    /**
     * 通过渠道子包ID获取渠道子包信息
     * @param $cpid
     * @return array
     * @throws \Cola\Exception\DbException
     */
    public function getChannelPackageInfoByCpid($cpid)
    {
        $sql = "select * from ops_channel_packages where cpid = '$cpid'";
        return $this->db('_backdb')->row($sql);
    }

    /**
     * 获取渠道信息
     * @return array
     * @throws \Cola\Exception\DbException
     */
    public function getChannelInfo($cid = '', $login=true)
    {
        !$cid && $cid = realCID();
        $field = "id,srid, cid, status, is_gm,is_open_pay,config";
        $sql = "select {$field},login_cid from ops_channels where cid = '$cid'";
        $channel = $this->db('_backdb')->row($sql);
        if ($channel && $login) {
            if ($channel['login_cid']) {
                $sql = "select {$field} from ops_channels where cid = '{$channel['login_cid']}'";
                $channel2 = $this->db('_backdb')->row($sql);
                $channel = ['cid'=>$channel2['cid']] + $channel;//还是使用自己的配置
            }
        }
        $channel && $channel['config'] = json_decode($channel['config'],true) ?? [];
        return $channel;
    }

    public function realAccount($account,$cid = '')
    {
        !$cid && $cid = $this->getChannelInfo()['cid'] ?? realCID();
        return $cid.'-'.$account;
    }


    public function getChannelInfoByLoginCid($cid)
    {

        $sql = "select cid from ops_channels where login_cid = '$cid'";
        $channel = $this->db('_backdb')->row($sql);
        $channel && $channel['config'] = json_decode($channel['config'],true) ?? [];
        return $channel;
    }

    public function checkCidIsLoginCid($cid,$cid2)
    {

        $sql = "select * from ops_channels where (login_cid = '$cid' and cid = '$cid2') or (login_cid = '$cid2' and cid = '$cid')";
        $channel = $this->db('_backdb')->row($sql);
        $channel && $channel['config'] = json_decode($channel['config'],true) ?? [];
        return $channel;
    }

    public function getAllCid($cid)
    {

        $sql = "select cid from ops_channels where login_cid = '$cid' ";
        $channel = $this->db('_backdb')->sql($sql);
        $channel && $cids = array_column($channel,'cid');
        $sql = "select login_cid from ops_channels where cid = '$cid' ";
        $channel2 = $this->db('_backdb')->sql($sql);
        $channel2 && $cids2 = array_column($channel2,'login_cid');
        return array_filter(array_merge([$cid],$cids??[],$cids2??[]));
    }

    public function getChannelInfoByCid($cid,$fields='*')
    {

        $sql = "select {$fields} from ops_channels where cid = '$cid' ";
        $channel = $this->db('_backdb')->row($sql);
        return $channel;
    }


    public function makeAliasConfig(){
        $sql = "select cid,sdk_alias from ops_channels where sdk_alias <> ''";
        $channelList = $this->db('_backdb')->sql($sql);
        $config = [];
        foreach ($channelList as $channel){
            $config[strtolower($channel['cid'])] = strtolower($channel['sdk_alias']);
        }

        $file =  CONFIG_PATH . '/alias_config/cid.php';
        self::generateConfigFile($file,$config,'cid');
        return include($file);
    }

    public function makeChannelConfig(){
        $sql = "select cpid,cid from ops_channel_packages";
        $cpids = $this->db('_backdb')->sql($sql);
        $config = [];
        foreach ($cpids as $cpid){
            $cids = array_filter(explode(',',$cpid['cid']));
            foreach ($cids as $cid){
                $config[$cid] = $cpid['cpid'];
            }
        }

        $file =  CONFIG_PATH . '/alias_config/channel.php';
        self::generateConfigFile($file,$config,'channel');
        return include($file);
    }

    public function getSDKList(){
        $directory = '../library/Application/pform';
        $folders = glob($directory . '/*', GLOB_ONLYDIR);

        $pform = [];
        // 输出文件夹名称
        foreach ($folders as $folder) {
            $pform[] = basename($folder);
        }

        return $pform;
    }

    public static function generateConfigFile($filePath, $config,$name='config') {
        $content = '<?php' . PHP_EOL;
        $content .= 'return ' . var_export($config, true) . ';' . PHP_EOL;

        file_put_contents($filePath, $content);
    }


}